(function() {
  "use strict";

  /**
   * @ngdoc directive
   * @name webApp.directive:rolelist
   * @description
   * # rolelist
   */
  angular.module("webApp").directive("memberChat", [
    "dataService",
    "$compile",
    function(dataService, $compile) {
      return {
        restrict: "E",
        replace: true,
        require: "ngModel",
        templateUrl: "views/directives.memberchat.html",
        controller: [
          "$templateCache",
          "$rootScope",
          "$scope",
          "$timeout",
          function($templateCache, $rootScope, $scope, $timeout) {
            //请求member信息
            $scope.member_id = 0;
            $scope.getMember = function(member_id) {
              if (member_id) {
                $scope.member_id = member_id;
                dataService
                  .memberGet({ query: { member_id: member_id } }, true)
                  .then(function(list) {
                    if (list.wx_members) {
                      $scope.member = list.wx_members[0];
                      console.error("mmmm", $scope.member);
                      $scope.timer = setTimeout(function() {
                        $scope.pageData().then(function() {
                          //   console.error("sssssssssssss");
                          //   var scroller = document.getElementById("autoscroll");
                          //   scroller.scrollTop = scroller.scrollHeight;
                        });
                      }, 5000);
                    }
                  });
              }
            };

            //清理定时器
            $scope.$on("$destroy", function() {
              if ($scope.timer) clearTimeout($scope.timer);
            });

            //请求聊天数据
            $scope.itemsTotal = 0;
            $scope.pageSize = 50;
            $scope.items = [];
            $scope.pageData = function(pageNo) {
              pageNo = pageNo ? pageNo : 1;
              var max_msg_id = "999999999999999999";
              var query = {
                query: {
                  member_open_id: $scope.member.open_id,
                  "chat_record_id.lt": max_msg_id
                },
                page_size: $scope.pageSize,
                order_by: "chat_record_id:asc",
                page_no: pageNo,
                is_read: true
              };

              console.error("queryquery", query);
              return dataService.chatRecordGet(query).then(function(data) {
                console.error("1111", data);
                $scope.items = data.chat_records;
                $scope.itemsTotal = data.total_count;
              });
            };
          }
        ],
        link: function(scope, element, attrs, ctrl) {
          //初始化
          ctrl.$render = function() {
            console.error("zzzz", ctrl.$viewValue);
            if (_.isNumber(ctrl.$viewValue)) {
              ctrl.$setViewValue(ctrl.$viewValue);
              scope.getMember(ctrl.$viewValue);
            }
          };
        }
      };
    }
  ]);
})();
